package com.zzc.service.impl;

import com.alibaba.fastjson.JSON;
import com.zzc.domain.Client;
import com.zzc.domain.ClientAttributes;
import com.zzc.repository.ClientAttributesRep;
import com.zzc.repository.ClientRep;
import com.zzc.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClientServiceImpl implements ClientService {

    @Autowired
    ClientRep clientRep;

    @Autowired
    ClientAttributesRep clientAttributesRep;

    @Override
    public List<Client> getAllClient() {
        return clientRep.findAll();
    }

    @Override
    public void addClient(Client client) {
        clientRep.save(client);
        System.out.println(client);
    }

    @Override
    public int getClientCount() {
        int count = (int) clientRep.count();
        System.out.println(count);
        return count;
    }

    @Override
    public void editClient(Client client) {
        clientRep.deleteByClientId(client.getClientId());
        clientRep.save(client);
    }

    @Override
    public Client findOneById(int clientId) {
        return clientRep.findByClientId(clientId);
    }

    @Override
    public String getClientAttributes(int clientId) {
        List<ClientAttributes> clientAttributes = clientAttributesRep.findByClientId(clientId);
        return JSON.toJSONString(clientAttributes);
    }
}
